package com.gzcsqic.excelapp.test;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.hutool.core.io.FileUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.util.Units;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.core.io.ClassPathResource;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {



    public static void main(String[] args) throws IOException {
        TemplateExportParams params = new TemplateExportParams("D:\\work\\idea_pro\\param\\template.xlsx",true);
//        TemplateExportParams params = new TemplateExportParams("D:\\模板\\模板.xlsx",true);
        Map<String,Object> map = new HashMap<>();
        map.put("title","啥事");
//
        XSSFWorkbook workbook = (XSSFWorkbook) ExcelExportUtil.exportExcel(params, map);
        XSSFSheet sheet = workbook.getSheet("Sheet1");
        int numberOfSheets = workbook.getNumberOfSheets();
        setPic(workbook,sheet,"d:/t3.png",0,1);
        setPic(workbook,sheet,"d:/t3.png",6,1);

        FileOutputStream fileOut = new FileOutputStream("d:/test1.xlsx");
        workbook.write(fileOut);
        fileOut.close();

//        XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(new File("d:/模板/test_param.xlsx")));
//        File file = new ClassPathResource("param/si_param.txt").getFile();
//        List<String> list = FileUtil.readLines(file, "UTF-8");
//        System.out.println(list);
    }

    static void setPic(XSSFWorkbook workbook,XSSFSheet sheet, String picPath, int startCol, int startRow) throws IOException {
        ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
        File image = new File(picPath);
        BufferedImage bufferImg = ImageIO.read(image);
        ImageIO.write(bufferImg, "png", byteArrayOut);
        XSSFDrawing patriarch = sheet.createDrawingPatriarch();

        // 获取单元格默认宽高
        Cell cell = sheet.getRow(1).createCell(0);
        int maxCol = 5,maxRow=16;
        double cellWidth = Math.ceil(sheet.getColumnWidthInPixels(cell.getColumnIndex()));
        double cellHeight = Math.ceil(cell.getRow().getHeightInPoints() / 72 * 96);

        // excel模板中图片区域的最大宽高
        double maxWidth = maxCol * cellWidth;
        double maxHeight = maxRow * cellHeight;
        // 图片文件的宽高
        BufferedImage sourceImg = ImageIO.read(image);
        double imageWidth = sourceImg.getWidth();
        double imageHeight = sourceImg.getHeight();

        double imgRate = imageWidth / imageHeight;
        if(imgRate >=1){ //横图
            int col2 = startCol + maxCol;
            double imgHeight = Math.ceil(maxWidth / (imageWidth / imageHeight));
            int rowCount = (int) Math.floor(imgHeight / cellHeight);
            startRow = startRow + (int)Math.ceil((maxRow-rowCount) / 2); // 设置起始y，垂直居中
            int row2 = startRow + rowCount;
            int dy2 = (int) (imgHeight -rowCount*cellHeight)+5;
            XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, XSSFShape.EMU_PER_PIXEL *dy2, (short) startCol, startRow, (short)col2, row2);
//            XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, Units.EMU_PER_PIXEL *dy2, (short) startCol, startRow, (short)col2, row2);
            patriarch.createPicture(anchor, workbook.addPicture(byteArrayOut.toByteArray(), XSSFWorkbook.PICTURE_TYPE_PNG));

        }else{ // 竖图
            double imgWidth = Math.ceil((imageWidth / imageHeight) * maxHeight);
            long colCount = (long) Math.floor(imgWidth / cellWidth);
            long row2 = startRow + maxRow;
            startCol = startCol + (int)Math.ceil((maxCol-colCount) / 2);
            int col2 = (int) (startCol + colCount);
            int dx2 = (int) (imgWidth - colCount * cellWidth);
//            XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, Units.EMU_PER_PIXEL *dx2, 0, (short) startCol, startRow, (short)col2, (int)row2);
            XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, XSSFShape.EMU_PER_PIXEL *dx2, 0, (short) startCol, startRow, (short)col2, (int)row2);
            patriarch.createPicture(anchor, workbook.addPicture(byteArrayOut.toByteArray(), XSSFWorkbook.PICTURE_TYPE_PNG));
        }
    }

}
